package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.vo.MallCategoryTreeVO;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "分类模块")
@Slf4j
@Validated
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 全部加载策略-分类树
     */
    @ApiOperation("获取分类树")
    @GetMapping("/tree")
    public JsonResult treeCategory(){
        log.debug("Controller-获取分类树");
        List<MallCategoryTreeVO> categoryTree = categoryService.treeCategory();
        log.debug("Controller-分类树获取成功");
        return JsonResult.ok(categoryTree);
    }

    /**
     * 延迟加载策略-顶级分类树
     */
    @ApiOperation("获取顶级分类树")
    @GetMapping("/tree/top")
    public JsonResult topTreeCategory(){
        log.debug("Controller-获取顶级分类树");
        List<MallCategoryTreeVO> topCategory = categoryService.treeTopCategory();
        log.debug("Controller-顶级分类树获取成功");
        return JsonResult.ok(topCategory);
    }
    /**
     * 延迟加载策略-子级分类树
     */
    @ApiOperation("获取子级类别树")
    @GetMapping("/tree/children/{parentId}")
    public JsonResult childrenTreeCategory(@PathVariable Long parentId){
        log.debug("Controller-获取子级分类树");
        List<MallCategoryTreeVO> childrenCategory = categoryService.treeChildrenCategory(parentId);
        log.debug("Controller-子级分类树获取成功");
        return JsonResult.ok(childrenCategory);
    }


    /**
     * 假数据模拟结果 mock
     * @return List<CategoryTreeVO>
     */
    private List<MallCategoryTreeVO> mock(){
        MallCategoryTreeVO vo = new MallCategoryTreeVO();
        vo.setId(1L);
        vo.setName("北京");

        MallCategoryTreeVO vo1 = new MallCategoryTreeVO();
        vo1.setId(2L);
        vo1.setName("海淀");

        List<MallCategoryTreeVO> children = new ArrayList<>();
        children.add(vo1);

        vo.setChildren(children);

        List result = new ArrayList();
        result.add(vo);

        return result;
    }
}
